<template>
  <div class="login-container">
    <el-card header="请先登入" class="login-card">
      <el-form @submit.native.prevent="login">
        <el-form-item label="用户名">
          <el-input v-model="model.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="model.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        model:{}
      }
    },
    methods: {
      async login() {
        const res = await this.$http.post('login',this.model)
        console.log(res);
        // 浏览器关掉后，token消失
        sessionStorage.token = res.data.token
        // 浏览器关掉后，token数据还在
        localStorage.token = res.data.token
        this.$router.push('/')
        this.$message({
          type:'success',
          message:'登录成功'
        })
        console.log(res);
      }
    },
  }
</script>

<style scoped>
.login-card {
  width: 25rem;
  margin: 5rem auto;
}
</style>